using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using Cit.CDE.Net.Module.Commons;
using cde.UserControl;
using Cit.CDE.Net.Module;

namespace UI.UserControl
{
    public class CssEditAttibutuITemplate : cde.UserControl.EditAttibutuITemplate
    {
        public CssEditAttibutuITemplate(string cname)
            : base(cname)
        {
        }

        public override void OnDataBinding(object sender, EventArgs e)
        {
            Label control = (Label)sender;

            DataGridItem container = (DataGridItem)control.NamingContainer;

            object value = ((DataRowView)container.DataItem)[colname];
            AttrbuteWithValue attribute = (AttrbuteWithValue)value;
            string text = "";
            CDEAttribute ca = attribute.Attribute;
            int size = CDEConstants.SIZE;
            int maxlength = CDEConstants.LENGTH;

            string s_readonly = "";
            if (CDEConstants.ConstDispType_READONLY.Equals(ca.DISP_type))
            {
                s_readonly = "readonly";
            }

            if (CDEConstants.ConstPointType_P_TARGET.Equals(ca.P_type))
            {
                List<object> listValue = (List<object>)attribute.Value;
                string hidValue = "";
                string txtValue = "";

                if (listValue != null)
                {
                    hidValue = StringWithXml.getObject2String(listValue[0]);
                    txtValue = StringWithXml.getObject2String(listValue[1]);
                }
                CDEAttribute s_ca = ca.getSAttribute();

                text = getHiddenText(colname + "_hid_" + ca.Name, colname + "_hid_" + s_ca.Name, hidValue);
                if (!CDEConstants.IsEmpty(ca.getP_Colmun().F_len))
                {
                    maxlength = int.Parse(ca.getP_Colmun().F_len);
                    size = (maxlength * CDEConstants.SIZE) / CDEConstants.LENGTH;
                }

                string align = "left";
                string css = "ronly";
                if (ConstDataType.NUMBER.Equals(ca.getP_Colmun().Type))
                {
                    css = "numronly";
                    align = "right";
                }
                css = css + " popup";
                css = css + " box_text";
                string expr = "onclick=\"show_point('" + ca.EntityManager.Full_Name + "',this)\"";
                text += getInputText(colname + "_txt_" + ca.Name, size, maxlength, txtValue, "readonly", align, css, expr);
            }
            else if (ca.getP_Colmun().isHaveCandidate())
            {
                if ("R".Equals(ca.Input_type))
                {
                    text = MultipleSelect.getRadioBoxs(colname + "_txt_" + ca.Name, ca.getP_Colmun().getCandidates(), StringWithXml.getObject2String(attribute.Value), "", "");
                }
                else
                {
                    text = getSelect(colname + "_txt_" + ca.Name,
                        ca.getP_Colmun().getCandidates(), StringWithXml.getObject2String(attribute.Value));
                }
            }
            else if (ConstDataType.STRING.Equals(ca.getP_Colmun().Type))
            {
                if (!CDEConstants.IsEmpty(ca.getP_Colmun().F_len))
                {
                    maxlength = int.Parse(ca.getP_Colmun().F_len);
                    size = (maxlength * CDEConstants.SIZE) / CDEConstants.LENGTH;
                }
                if ("TA".Equals(ca.Input_type))
                {
                    int rows = 2;
                    int cols = 25;
                    try
                    {
                        rows = int.Parse(ca.Input_rows);
                    }
                    catch { };
                    try
                    {
                        cols = int.Parse(ca.Input_cols);
                    }
                    catch { };
                    text = getTextarea(colname + "_txt_" + ca.Name, rows, cols, StringWithXml.getObject2String(attribute.Value), s_readonly, "", "box_text");
                }
                else
                {
                    text = getInputText(colname + "_txt_" + ca.Name, size, maxlength, StringWithXml.getObject2String(attribute.Value), s_readonly, "", "box_text");
                }
            }
            else if (ConstDataType.NUMBER.Equals(ca.getP_Colmun().Type))
            {
                if (!CDEConstants.IsEmpty(ca.getP_Colmun().F_len))
                {
                    maxlength = int.Parse(ca.getP_Colmun().F_len);
                    size = (maxlength * CDEConstants.SIZE) / CDEConstants.LENGTH;
                }
                text = getInputText(colname + "_txt_" + ca.Name, size, maxlength, StringWithXml.getObject2String(attribute.Value), s_readonly, "right", "box_text");
            }
            else if (ConstDataType.DATE.Equals(ca.getP_Colmun().Type))
            {
                if (s_readonly == "")
                {
                    text = getHiddenText(colname + "_hid_" + ca.Name, colname + "_hid_" + ca.Name, "SYS_DATE_CLM");
                }
                text += getInputText(colname + "_txt_" + ca.Name, CDEConstants.SIZE, CDEConstants.LENGTH, StringWithXml.getObject2String(attribute.Value), s_readonly, "right", "box_text");
            }
            else if (ConstDataType.BOOLEAN.Equals(ca.getP_Colmun().Type))
            {
                text = getBoolSelect(colname + "_txt_" + ca.Name, StringWithXml.getObject2String(attribute.Value));
            }
            else
            {
                text = StringWithXml.getObject2String(attribute.Value);
            }

            string err = attribute.Err;
            if (!CDEConstants.IsEmpty(err))
            {
                //text = text + "&nbsp;<img src=\"../../images/err_mark.gif\" width=\"12\" alt=\"" + err + "\" />";
                text = text + "&nbsp;<font color=\"red\">" + err + "</font>";
            }
            control.Text = text;
        }
    }
}
